package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.AdminTopicService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/topic")
public class AdminTopicController extends HttpServlet {


    @Autowired
    AdminTopicService adminTopicService;


    @RequiresPermissions("admin:topic:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"},button = "阅读")
    @GetMapping("read")
    public Object read(Integer id) {
        Map<String,Object> data=adminTopicService.read(id);
        Object ok = ResponseUtil.ok(data);
        return ok;
    }


    @GetMapping("list")
    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"},button = "查询")
    public Object list(Integer page, Integer limit, String sort, String order, String title, String subtitle) {

        List<MarketTopic> userList = adminTopicService.list(page, limit, sort, order,title, subtitle);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(userList);
        return o;
    }



    @PostMapping("update")
    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"},button = "编辑")
    public Object update(@RequestBody MarketTopic marketTopic) {
        int i=adminTopicService.update(marketTopic);
        Object o = ResponseUtil.ok(i);
        return o;
    }

    @PostMapping("delete")
    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"},button = "删除")
    public Object delete(@RequestBody Map params) {
        Integer id = (Integer) params.get("id");
        int i=adminTopicService.delete(id);
        Object o = ResponseUtil.ok(i);
        return o;
    }

    @PostMapping("create")
    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"},button = "添加")
    public Object create(@RequestBody MarketTopic marketTopic) {
        int i=adminTopicService.create(marketTopic);
        Object o = ResponseUtil.ok(i);
        return o;
    }
}
